{include file="public/header" /}
<body class="gray-bg">
<div class="wrapper wrapper-content">
    <div class="ibox float-e-margins">
        <div class="ibox-content">
            <div class="row">
                <!--搜索框开始-->
                {if session('uid') == 1}
                <div class="col-sm-12">
                    <div class="layui-form layui-form-item">
                        <div class="layui-input-inline layui-form">
                            <select name="area" lay-search="">
                                <option value="">请选择地区</option>
                                <option value="0" >admin</option>
                                {if !empty($data)}
                                {foreach name="data" item="vo"}
                                <option value="{$vo.district_id}" >{$vo.district}</option>
                                {/foreach}
                                {/if}
                            </select>
                        </div>
                        <div class="layui-input-inline">
                            <button class="layui-btn" lay-submit="" lay-filter="LAY-search">立即搜索</button>
                            <button  class="layui-btn layui-btn-normal" id="empty"  lay-submit="" lay-filter="LAY-search">重置</button>
                        </div>
                    </div>
                </div>
                {/if}
                <!--搜索框结束-->
                <!--操作框开始-->
                <div class="col-sm-12">
                    <div class="input-group" >
                        <button class="btn btn-primary" type="button" onclick="wk.href('{:url('adAdd')}');">
                            <i class="fa fa-plus"></i> 添加广告图
                    </div>
                </div>
                <!--操作框结束-->
            </div>
            <div class="hr-line-dashed"></div>

            <div class="example-wrap">
                <div class="example">
                    <table class="layui-hide" id="test" lay-filter="demo"></table>
                    <!--图片模板-->
                    <script type="text/html" id="imgTpl">
                        <img src="{{d.ad_img}}" class="img-circle table-img" onerror="this.src='/static/admin/images/head_default.gif'"  />
                    </script>
                    <!--角色模板-->
                    <script type="text/html" id="titleTpl">
                        {{# if(d.ad_type == 1){ }}
                        总部
                        {{# }else{ }}
                        区代理
                        {{# } }}
                    </script>
                    <script type="text/html" id="portTpl">
                        {{# if(d.ad_port == 1){ }}
                        用户首页
                        {{# }else if(d.ad_port == 2){ }}
                        商户首页
                        {{# }else{ }}
                        社交圈
                        {{# } }}
                    </script>
                    <script type="text/html" id="cateTpl">
                        {{# switch(d.ad_cate){ case 1: }}
                        链接
                        {{# break;case 2: }}
                        店铺
                        {{# break;case 3: }}
                        商品
                        {{# break;case 4: }}
                        积分活动
                        {{# break;default: }}
                        动态
                        {{# } }}
                    </script>
                    <script type="text/html" id="posTpl">
                        {{# switch(d.ad_postion){ case 1: }}
                        上部
                        {{# break;case 2: }}
                        下部
                        {{# break;case 3: }}
                        默认
                        {{# } }}
                    </script>
                    <!--操作模板-->
                    <script type="text/html" id="opeBar">
                        <a href="javascript:;" onclick="wk.href('{:url('adEdit')}?id={{d.ad_id}}')" class="btn btn-primary btn-xs m-r-xs" title="编辑"><i class="fa fa-pencil"></i></a>
                        <a href="javascript:;" onclick="wk.confirm('{{d.ad_id}}','{:url('adDel')}')" class="btn btn-danger btn-xs m-r-xs" title="删除"><i class="fa fa-trash-o"></i></a>
                        <a href="javascript:;" onclick="son({{d.ad_id}})" class="btn btn-primary btn-xs m-r-xs" >排序</a>
                    </script>
                </div>
            </div>
        </div>
    </div>
</div>
</div>
{include file="public/footer" /}
<script>
    //layui渲染数据表格
    layui.use('table', function () {
        var table = layui.table
            , form = layui.form;
        table.render({
            elem: '#test'
            , url: '{:url("advertise/index")}'
            ,response: {
                statusCode: 220 //成功的状态码，默认：0
            }
            , page: true
            , size: 'lg' //sm小尺寸的表格 lg大尺寸
            , cellMinWidth: 150
            , limits: [5, 10, 15, 20, 25, 30]
            , limit: 10
            , loading: true
            , id: 'testReload'
            , cols: [[
                {type:'numbers', width: 80, title: '序号'}
                , {field: 'ad_type', width: '', title: '角色', align: 'center',templet:'#titleTpl'}
                , {field: 'ad_port', width: '', title: '终端', align: 'center',templet:'#portTpl'}
                , {field: 'ad_postion', width: '', title: '位置', align: 'center',templet:'#posTpl'}
                , {field: 'username', width: '', title: '发布人', align: 'center'}
                , {field: 'ad_img', width: 90, title: '图片', align: 'center', templet: '#imgTpl'}
                , {field: 'ad_cate', width: '', title: '链接类型', align: 'center', templet: '#cateTpl'}
                , {field: 'ad_link', width: '', title: '链接', align: 'center'}
                , {field: 'ad_left_title', width: '', title: '左标题', align: 'center'}
                , {field: 'ad_right_title', width: '', title: '右标题', align: 'center'}
                , {field: 'ad_add_time', width: '', title: '添加时间', align: 'center'}
                , {field: 'ad_edit_time', width: '', title: '编辑时间', align: 'center'}
                , {fixed: 'right', field:'ad_sort', width: 80, title: '排序值', align: 'center'}
                , {fixed: 'right', width: 150, title: '操作', align: 'center', toolbar: '#opeBar'}
            ]],
            done: function (res, curr, count) {
                $('.img-circle').viewer();
            }
        });
    });

    function son(shop_id) {
        layer.prompt({
            formType: 2,
            title: '修改排序值',
            area: ['200px', '50px'],
            btn: ['确认', '取消'],
            maxlength:11,
        },function(value, index, elem){
            var pattern = /^[1-9]\d*$/;
            if(!pattern.test(value)){
                wk.error('请填写正确的排序值');
                return;
            }
            send1(shop_id,value);
            layer.close(index);
        });
    }
    function send1(shop_id,value){
        $.ajax({
            url: "{:url('addSort')}",
            type: "post",
            dataType: "json",
            data: {id:shop_id,num:value},
            success: function (res) {
                if(res.code == 200){
                    layer.msg(res.msg,{icon:1,time:1500,shade: 0.1},function(){
                        //重载当前页表格
                        $(".layui-laypage-btn").click();
                    });
                } else if (res.code == 100){
                    layer.msg(res.msg,{icon:2,time:1500,shade: 0.1});
                }
            }
        });
    }
</script>
</body>
</html>